/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.dropdown;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.xuexiang.xui_lib.component.GridLayout;
import com.xuexiang.xui_lib.util.UntilTNT;
import com.xuexiang.xui_lib.util.ViewModel;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.provider.DropGridprovider;
import com.xuexiang.xuidemo.provider.DropProvider;
import com.xuexiang.xuidemo.view.DirectionLaytionTextview;
import com.xuexiang.xuidemo.view.DropDownMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * 下拉框第二个导航页面
 *
 * @since 2021-03-30
 */
public class DropdownTwoSlice extends AbilitySlice implements
    GridLayout.OnItemClickListener,
    DirectionLaytionTextview.CallbackClick {
    private DropDownMenu dropDownMenu;
    private DirectionLaytionTextview titleview;
    private ListContainer listconter;
    private ListContainer listconter1;
    private ListContainer listconter2;
    private Context context;
    private List<Component> arrayct = new ArrayList<>();
    private String[] data;
    private String[] data1;
    private String[] data2;
    private String[] data3;
    private AbilitySlice abilitySlice;
    private Component lastComponent;
    private List<UntilTNT> list;
    private List<UntilTNT> arrayList;
    private List<UntilTNT> array2;
    private DropProvider dropProvider;
    private DropProvider dropProvider1;
    private DropProvider dropProvider2;
    private final int anInt = -1;
    private final int anInt1 = 1;
    private final int anInt2 = 2;
    private final int anInt3 = 3;
    private final int anInt4 = 4;
    private final int anInt10 = 10;
    private final int anInt11 = 11;
    private final int anInt12 = 12;

    /**
     * 页面加载
     *
     * @param intent
     */
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_dropdown_twolayout);
        context = this;
        abilitySlice = this;
        ViewModel viewModel = new ViewModel(this);
        data = viewModel.getStringResource(ResourceTable.Strarray_city_entry); // 城市
        data1 = viewModel.getStringResource(ResourceTable.Strarray_age_entry); // 年龄
        data2 = viewModel.getStringResource(ResourceTable.Strarray_sex_entry); // 性别
        data3 = viewModel.getStringResource(ResourceTable.Strarray_constellation_entry); // 星座

        initview();
        initdata();
        setListconter();
        setTwoview();
    }

    private void initview() {
        dropDownMenu = (DropDownMenu) findComponentById(ResourceTable.Id_dropview);
        titleview = (DirectionLaytionTextview) findComponentById(ResourceTable.Id_titleview);
        titleview.settitle("DropDownMenu", "下拉选择菜单");
        titleview.setright(anInt1, 0);
        titleview.setLeftLayout(anInt4, ResourceTable.Media_icon_back_white);
        titleview.setclick(this::setComponent);
        listconter = new ListContainer(context);
        listconter.setId(anInt10);
        listconter.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        listconter.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
    }

    private void initdata() {
        list = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            UntilTNT until = new UntilTNT();
            until.setName(data[i]);
            until.setType(0);
            list.add(until);
        }
        dropProvider = new DropProvider(list, context);
        listconter.setItemProvider(dropProvider);
        listconter.setVisibility(Component.HIDE);
        listconter1 = new ListContainer(context);
        listconter1.setId(anInt11);
        listconter1.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        listconter1.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);

        arrayList = new ArrayList<>();
        for (int i = 0; i < data1.length; i++) {
            UntilTNT until = new UntilTNT();
            until.setName(data1[i]);
            until.setType(anInt1);
            arrayList.add(until);
        }
        dropProvider1 = new DropProvider(arrayList, context);
        listconter1.setItemProvider(dropProvider1);
        listconter1.setVisibility(Component.HIDE);
        listconter2 = new ListContainer(context);
        listconter2.setId(anInt12);
        listconter2.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        listconter2.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        array2 = new ArrayList<>();
        for (int i = 0; i < data2.length; i++) {
            UntilTNT until = new UntilTNT();
            until.setName(data2[i]);
            until.setType(1);
            array2.add(until);
        }
        dropProvider2 = new DropProvider(array2, context);
        listconter2.setItemProvider(dropProvider2);
        listconter2.setVisibility(Component.HIDE);
    }

    private void setListconter() {
        Component component = LayoutScatter.getInstance(context)
            .parse(ResourceTable.Layout_gridlayoutbutton_view, null, false);
        GridLayout gridLayout = (GridLayout) component.findComponentById(ResourceTable.Id_gridview);
        Button butview = null;
        butview = (Button) component.findComponentById(ResourceTable.Id_butgrid);
        gridLayout.setSpanCount(anInt3);
        component.setVisibility(Component.HIDE);
        gridLayout.setOnItemClickListener(this);
        gridLayout.setSpacing(AttrHelper.vp2px(anInt2, context));
        gridLayout.setProvider(new DropGridprovider(context) {
            @Override
            protected String[] getTitle() {
                return data3;
            }
        });
        butview.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dropDownMenu.setFourhide();
            }
        });
        arrayct.add(listconter);
        arrayct.add(listconter1);
        arrayct.add(listconter2);
        arrayct.add(component);
        dropDownMenu.setCompoent(arrayct);

        listconter.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                for (int j = 0; j <= list.size() - 1; j++) {
                    UntilTNT until = list.get(j);
                    until.setmSelectPosition(anInt);
                }
                UntilTNT until = (UntilTNT) listContainer.getItemProvider().getItem(i);
                String ctndata = until.getName();
                dropDownMenu.setTabMenuText(0, ctndata); // 设置第几个数字
                until.setmSelectPosition(i);
                list.set(i, until);
                dropProvider.notifyDataChanged();
            }
        });
    }

    private void setTwoview() {
        listconter1.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                for (int j = 0; j <= arrayList.size() - 1; j++) {
                    UntilTNT until = arrayList.get(j);
                    until.setmSelectPosition(anInt);
                }
                UntilTNT until = (UntilTNT) listContainer.getItemProvider().getItem(i);
                String ctndata = until.getName();
                dropDownMenu.setTabMenuText(anInt1, ctndata); // 设置第几个数字
                until.setmSelectPosition(i);
                arrayList.set(i, until);
                dropProvider1.notifyDataChanged();
            }
        });
        listconter2.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int post, long l) {
                for (int j = 0; j <= array2.size() - 1; j++) {
                    UntilTNT until = array2.get(j);
                    until.setmSelectPosition(anInt);
                }
                UntilTNT until = (UntilTNT) listContainer.getItemProvider().getItem(post);
                String ctndata = until.getName();
                dropDownMenu.setTabMenuText(anInt2, ctndata);
                until.setmSelectPosition(post);
                array2.set(post, until);
                dropProvider2.notifyDataChanged();
            }
        });
    }

    @Override
    public void onItemClick(Component component, int position) {
        if (lastComponent != null) {
            Text text = (Text) lastComponent.findComponentById(ResourceTable.Id_textid);
            text.setTextColor(new Color(Color.getIntColor("#FF7E7E7E")));
            DirectionalLayout directionalLayout = null;
            directionalLayout = (DirectionalLayout) lastComponent.findComponentById(ResourceTable.Id_direlayout);
            Element element = ElementScatter.getInstance(context).parse(ResourceTable.Graphic_selector_bg_tag_stroke);
            directionalLayout.setBackground(element);
        }
        Text text = (Text) component.findComponentById(ResourceTable.Id_textid);
        DirectionalLayout directionalLayout = null;
        directionalLayout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_direlayout);
        Element element = ElementScatter.getInstance(context).parse(ResourceTable.Graphic_selector_page2);
        directionalLayout.setBackground(element);
        text.setTextColor(new Color(Color.getIntColor("#299EE3")));
        dropDownMenu.setTabMenuText(anInt3, data3[position]);
        lastComponent = component;
    }

    @Override
    public void setComponent(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_img1:
                abilitySlice.terminate();
                break;
            default:
                break;
        }
    }
}
